﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IdleTest.DemoToUse
{
    [TestClass]
    class DataProvider : IDisposable
    {
        private static DataProvider instance;
        public static DataProvider Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataProvider();
                }
                return instance;
            }
        }

        public string[] DefaultColumns
        {
            get { return new string[] { "colString", "colInt" }; }
        }

        private DataRow dataRow;
        public DataRow DataRow
        {
            get
            {
                if (dataRow == null)
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("colString", "string");
                    dic.Add("colInt", 123);
                    dataRow = CreateDataRow(dic);
                }

                return dataRow;
            }
        }

        private DataTable table;
        public DataTable Table
        {
            get
            {
                if (table == null)
                {
                    Dictionary<string, List<object>> dic = new Dictionary<string, List<object>>();
                    dic.Add("colString", new List<object>{"string1", "string2"});
                    dic.Add("colInt", new List<object>{111, 123});
                    table = CreateTable(dic);
                }
                return table;
            }
        }

        public DataSet DataSet
        {
            get
            {
                Dispose();
                DataSet ds = new DataSet();
                ds.Tables.Add(this.Table);
                return ds;
            }
        }

        private DataRow CreateDataRow(Dictionary<string, object> values)
        {
            DataRow dr = null;
            if (values == null || values.Count == 0)
            {
                return dr;
            }

            DataTable dt = new DataTable();
            foreach(KeyValuePair<string,object> item in values)
            {
                dt.Columns.Add(item.Key);
            }

            dr = dt.NewRow(); 
            foreach (KeyValuePair<string, object> item in values)
            {
                dr[item.Key] = item.Value;
            }

            return dr;
        }

        public DataTable CreateTable(Dictionary<string, List<object>> values)
        {
            DataTable dt = null;
            if (values == null || values.Count == 0)
            {
                return dt;
            }

            dt = new DataTable();
            foreach (KeyValuePair<string, List<object>> item in values)
            {
                dt.Columns.Add(item.Key);
            }

            DataRow dr = null;
            
            int rowCount = values.FirstOrDefault().Value.Count;
            for (int i = 0; i < rowCount; i++)
            {
                dr = dt.NewRow();
                foreach (KeyValuePair<string, List<object>> item in values)
                {
                    dr[item.Key] = item.Value[0];
                }
                dt.Rows.Add(dr);
            }

            return dt;
        }

        public void Dispose()
        {
            this.dataRow = null;
            this.table = null;
        }
    }
}
